package com.fzl.spring_chatroom.service;

import com.fzl.spring_chatroom.model.dao.Friend;
import com.fzl.spring_chatroom.model.mapper.FriendMapper;
import com.fzl.spring_chatroom.model.response.FriendInfoResponse;
import com.fzl.spring_chatroom.utils.BeanTransUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FriendService {
    @Autowired
    private FriendMapper friendMapper;

    public List<FriendInfoResponse> getFriendList(Integer userId) {
        List<Friend> friends = friendMapper.selectFriendList(userId);
        List<FriendInfoResponse> res = new ArrayList<>();
        for (Friend f : friends) {
            FriendInfoResponse r = new FriendInfoResponse();
            r.setFriendId(f.getFriendId());
            r.setFriendName(f.getFriendName());
            String avatar = (f.getAvatar() == null || f.getAvatar().isEmpty()) ? "/avatar/default.png" : f.getAvatar();
            r.setAvatar(avatar);
            res.add(r);
        }
        return res;
    }

    public boolean isFriend(Integer userId, Integer friendId) {
        return friendMapper.isFriend(userId, friendId) > 0;
    }
}
